//
//  StatisticViewController.m
//  e-nurse
//
//  Created by  on 10/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "StatisticViewController.h"
#import "DateUtils.h"
#import "Database.h"
#import "Record.h"
#import "ServerProxy.h"

@interface StatisticViewController ()

@property (strong, nonatomic) NSArray* records;

-(void)updateChart;

@end

@implementation StatisticViewController
@synthesize timeSegmentControl = _timeSegmentControl;
@synthesize records = _records;

-(void)dealloc
{
    self.records = nil;
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSDate* currentDate = [[NSDate alloc] init];
    NSDate* startDate = nil;
    NSDate* endDate = nil;
    [DateUtils getWeeksRange:(&startDate) endDay:(&endDate) currentDay:currentDate weekCount:1];
    [currentDate release];
    
    self.records = [[Database sharedInstance] getRecordsByDate:startDate endDay:endDate userId:[[ServerProxy sharedInstance] currentUserId]];
    
    [self updateChart];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    self.timeSegmentControl = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)timeSegChanged:(id)sender
{
    [self updateChart];
}

-(void)updateChart
{
    switch ([self.timeSegmentControl selectedSegmentIndex]) {
        case 0://Day
        {
            for (Record* record in self.records) 
            {
                if([DateUtils isDayOfDate:record.date]){
                    //Add to the chart.
                }
            }
        }            break;
        case 1://All
        {
            for (Record* record in self.records) 
            {
                //Add to the chart.
            }
        }           
            break;
        case 2://Night
        {
            for (Record* record in self.records) 
            {
                if([DateUtils isNightOfDate:record.date]){
                    //Add to the chart.
                }
            }
        }            
            break;
        default:
            break;
    }
}

@end
